package tableprocessor;

import java.util.Iterator;
import java.util.List;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author gleb
 */
public class DBSupplierDAO
{
    private final static Logger logger = LoggerFactory.getLogger(DBSupplierDAO.class);
    public static Iterator<DBSupplier> getSupplierList()
    {
        Transaction tx = null;
        List <DBSupplier> rlist = null;
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        try
        {
            session.setFlushMode(FlushMode.NEVER);
            tx = session.beginTransaction();
            Query q = session.createQuery("FROM DBSupplier");//"SELECT rlist FROM DBCathegory AS rlist ");
            rlist = q.list();
            tx.commit();
            return rlist.iterator();
        }
        catch (RuntimeException e)
        {
            if (tx != null && tx.isActive())
            {
                try {tx.rollback();}
                catch (HibernateException e1){}
            }
        }
        return null;
    }
    public static void save(String name, String adress, String phone, String region)
    {
        Transaction tx = null;
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        try
        {
           // session.setFlushMode(FlushMode.NEVER);
            tx = session.beginTransaction();
            session.save(new DBSupplier(name, adress, phone, region));
            tx.commit();
        }
        catch (RuntimeException e)
        {
            if (tx != null && tx.isActive())
            {
                try {tx.rollback();}
                catch (HibernateException e1){}
            }
        }
    }
}
